package com.darren.euc.action;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.darren.euc.entity.Client;
import com.darren.euc.entity.Clue;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.User;
import com.darren.euc.service.ClientService;
import com.darren.euc.util.PropertyHelper;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/client")
public class ClientAction {
	
	private static final Logger log = Logger.getLogger(ClientAction.class);
	
	@Autowired
    private ClientService clientService;
	
	/**
	 * 运营管理，会员中心，只查询已签约客户和，已入驻客户用来做会员管理
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/list")//,method = RequestMethod.POST)
    public List<Client> getAll(HttpServletRequest request,HttpServletResponse response) {
		List<Client> clients = clientService.getActiveclient(); 
    	for(Client client:clients){
    		if(client.getOrderList() != null){
    			client.getOrderList().clear();
    		}
    		if(client.getClientExtensionList() != null){
    			client.getClientExtensionList().clear();
    		}
    	}
        return clients;
    }
	/**
	 * 运营管理，发送站内信，查找目标客户，只查已入驻，未离场的客户
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/activeclient",method = RequestMethod.POST)
    public List<Client> getActiveclient(HttpServletRequest request,HttpServletResponse response) {
    	List<Client> clients = clientService.getActiveclient(); 
    	for(Client client:clients){
    		if(client.getOrderList() != null){
    			client.getOrderList().clear();
    		}
    		if(client.getClientExtensionList() != null){
    			client.getClientExtensionList().clear();
    		}
    	}
        return clients;
    }
	
	/**
	 * 运营管理，会员中心，添加会员时，选择所属客户列表
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/memberclient",method = RequestMethod.POST)
    public List<Client> getMemberClient(HttpServletRequest request,HttpServletResponse response) {
    	List<Client> clients = clientService.getMemberClient(); 
    	for(Client client:clients){
    		if(client.getOrderList() != null){
    			client.getOrderList().clear();
    		}
    		if(client.getClientExtensionList() != null){
    			client.getClientExtensionList().clear();
    		}
    	}
        return clients;
    }
	
	@ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public ResponseJSON queryClient(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		User user = (User) request.getAttribute("user");
		JSONObject data = requestJson.getJSONObject("data");
		data.put("userId", user.getId());
		PageBean pageBean = clientService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/signclient",method = RequestMethod.POST)
    public ResponseJSON queryCluesWithOrder(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		User user = (User) request.getAttribute("user");
		JSONObject data = requestJson.getJSONObject("data");
		data.put("userId", user.getId());
		PageBean pageBean = clientService.queryCluesWithOrder(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	/**
	 * 客户列表
	 * @param requestJson
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/clients",method = RequestMethod.POST)
    public ResponseJSON queryCluesWithClient(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = clientService.queryCluesWithClient(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	/**
	 * 已入驻客户
	 * @param requestJson
	 * @param request
	 * @param response
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/enterclient",method = RequestMethod.POST)
    public ResponseJSON queryCluesWithEnterClient(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = clientService.queryCluesWithEnterClient(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/signclientbyname",method = RequestMethod.POST)
    public List<Clue> queryCluesByClientName(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
		String clientName = requestJson.getString("clientName");
		return clientService.queryCluesByClientName(clientName);
    }
	
	@ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseJSON saveClient(@RequestBody Client client,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		try {
			client = clientService.save(client);
			responseJson.setStatus(0);
			responseJson.setMessage("添加成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("添加失败！");
		}
		responseJson.setData(client);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseJSON deleteRole(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
    	ResponseJSON responseJson = new ResponseJSON();
    	JSONObject jsonData = new JSONObject();
    	responseJson.setData(jsonData);
    	try{
    		Integer id = Integer.parseInt(requestJson.getString("id"));    		
    		Client client = new Client();
    		client.setId(id);
    		clientService.delete(client);
        	responseJson.setStatus(0);
    		responseJson.setMessage("删除成功！");
    	}catch(Exception ex){
    		log.info(ex.getMessage(),ex);
        	responseJson.setStatus(-1);
    		responseJson.setMessage("删除失败！");
    	}
    	System.out.println("==============================================================");
    	System.out.println(requestJson.toString());
    	System.out.println(responseJson.toString());
    	System.out.println("==============================================================");
        return responseJson;
    }
	  
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseJSON updateClient(@RequestBody Client client, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {				
			User user = (User) request.getAttribute("user");
			if(user != null){				
				client.setLastModifyUserId(user.getId());
			}
			client.setLastModifyTime(new Date());
			client = clientService.update(client);			
			responseJson.setStatus(0);
			responseJson.setMessage("保存成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(client);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}

	
}